package com.chinaoryx.er;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: hyman.zhang
 * Date: 12-1-28
 * Time: 上午10:48
 * To change this template use File | Settings | File Templates.
 */
public class EntityDefination {

    // pri table
    private TableDefination table;
    
    private TableColumn primaryKey;

    public TableColumn getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(TableColumn primaryKey) {
        this.primaryKey = primaryKey;
    }
    // where.
    //....




    public void loadSchema(Connection conn) throws SQLException {
        this.loadTableSchema(conn, this.table);
        this.initSql();

    }
    
    private void loadTableSchema(Connection conn, TableDefination table) throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from " + table.getTableName() + " where 1=0");
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        String[] columnNames = new String[columnCount];
        for (int i = 0; i<columnCount; i++) {
            columnNames[i] = rsmd.getColumnLabel(i+1);
        }
        table.setColumnNames(columnNames);
        stmt.close();




        List<TableDefination> subTables = table.getSubTables();
        if (subTables != null) {
            for (TableDefination subTable: subTables) {
                this.loadTableSchema(conn, subTable);
            }
        }
        

        
    }

    public String getSqlForList(){
        //TODO how to generate the criteria
        StringBuilder sb = new StringBuilder();
        sb.append("select * ");
        sb.append(" from ");
        sb.append(this.table.getTableName());
        //sb.append(" where xxxxxx");
        sb.append(" limit 500");
        return sb.toString();
    }



    public void deleteEntity(Connection conn, Map<String, Object> result) throws SQLException {
        
        PreparedStatement pstmt = null;
        List<String> deleteSqlList = new ArrayList<String>();
        this.table.genDeleteSql(deleteSqlList);
        
        // generate the delete criteria
        StringBuilder sqlWhere = new StringBuilder();
        sqlWhere.append(" where ");
        sqlWhere.append(this.table.getAliasName());
        sqlWhere.append(".");
        sqlWhere.append(this.primaryKey.getColumnName());
        sqlWhere.append("=?");

        
        for (String deleteSql: deleteSqlList) {
            StringBuilder sql = new StringBuilder();
            sql.append(deleteSql);
            sql.append(sqlWhere);
            
            pstmt = conn.prepareStatement(sql.toString());
            Object pkValue = result.get(this.primaryKey.getColumnName().toUpperCase());
            pstmt.setObject(1, pkValue);
            System.out.println(sql.toString());
            pstmt.execute();

            pstmt.close();
            
        }


    }


    public void initSql(){
        this.initInsertSql(this.table);
    }

    private void initInsertSql(TableDefination table) {

        table.initInsertSql();
        List<TableDefination> subTables = table.getSubTables();
        if (subTables != null) {
            for (TableDefination subTable: subTables) {
                     this.initInsertSql(subTable);
            }
        }
    }

    public TableDefination getTable() {
        return table;
    }

    public void setTable(TableDefination table) {
        this.table = table;
    }


}
